var map = function() {
};

map.prototype = {
	cMap : {},
	put : function(key, value) {
		this.cMap[key] = value;
	},
	get : function(key) {
		return this.cMap[key];
	},
	remove : function(key) {
		delete this.cMap[key];
	}
};

(function() {
	if (!window.plus) {
		var p = function() {
		};

		p.prototype = {
			plusReady : function(Callee) {
				if (!this.OnReadyCallees) {
					this.OnReadyCallees = [];
				}
				this.OnReadyCallees[this.OnReadyCallees.length] = Callee;
			}
		};

		window.plus = new p();
		window.addEventListener("load", function() {
			if (window.plus.OnReadyCallees) {
				window.plus.OnReadyCallees.forEach(function(item, index, array) {
					if (typeof item == "function") {
						item();
					}
				});
			}
		});
	}
})();

var cm = function() {
	this.initialize();
};

cm.prototype = {
	cbMap : {},
	initialize : function() {
		this.cbMap = new map();
		this.TYPE_SUCCESS = "success";
		this.TYPE_ERROR = "error";
		this.ONCE = "once";
	},
	// 自动生成key时调用
	putAndGetKey : function(onSuccess, onFailed, once) {
		var currentDate = new Date();
		var key = "cm" + currentDate.getTime();
		this.putByKey(key, onSuccess, onFailed, once);
		return key;
	},
	// 自己指定key时调用
	putByKey : function(key, onSuccess, onFailed, once) {
		var success = (typeof onSuccess !== 'function' ? null : function(args) {
			onSuccess(args);
		});
		var fail = (typeof onFailed !== 'function' ? null : function(code) {
			onFailed(code);
		});
		var remove = false;
		if (typeof (once) == "boolean") {
			remove = once;
		}
		var value = {};
		value[this.TYPE_SUCCESS] = success;
		value[this.TYPE_ERROR] = fail;
		value[this.ONCE] = remove;
		this.cbMap.put(key, value);
	},
	// 根据key获取
	getByKey : function(key) {
		return this.cbMap.get(key);
	},
	removeByKey : function(key) {
		this.cbMap.remove(key);
	}
};

var funM = new cm();

// bridge
(function() {
	if (!plus.bridge) {
		var b = function() {
		};
		b.prototype = {
			exec : function(interfaceName, mathodName, param, onSuccess, onFailed, once) {
				try {
					if (window.bridge) {
						var key = funM.putAndGetKey(onSuccess, onFailed, once);
						window.bridge.exec(interfaceName, mathodName, key, JSON.stringify([ param ]));
					} else {
						exec(interfaceName, mathodName, param);
					}
				} catch (e) {
					console.log(e);
				}
			}
		};
		window.plus.bridge = new b();
	}
})();

// JsExecutor
(function() {
	if (!plus.JsExecutor) {
		var executor = function() {
		};
		executor.prototype = {
			exec : function(param) {
				var order = param; // JSON.parse(param);
				var key = order.key;
				var result = order.result;
				var state = order.state;
				this.execCallback(key, result, state)
			},
			execCallback : function(key, result, state) {
				var callBack = funM.getByKey(key);
				if (!callBack) {
					console.log("callBack key: " + key + " 不存在");
					return;
				}
				var remove = callBack[funM.ONCE];
				if (typeof (remove) == "boolean") {
					if (remove)
						funM.removeByKey(key);
				}
				if (typeof (state) == "boolean") {
					var fuc = callBack[state ? funM.TYPE_SUCCESS : funM.TYPE_ERROR];
					if (typeof (fuc) == "function") {
						fuc(decodeURI(result));
					}
				}
			}
		};
		window.plus.jsExecutor = new executor();
	}
})();